﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Benefit.Service.Tools
{
    public class ServiceWorker : Interface.Tools.IWorker
    {
        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.Sys.IServerStatus IServerStatus = null;
        private Interface.Sys.IExchange IExchange = null;
        private Interface.Sys.IBroker IBroker = null;
        private Interface.Sys.IProduct IProduct = null;
        private Interface.Sys.IInstrument IInstrument = null;
        private Interface.Sys.IAccount IAccount = null;
        private Interface.ServerData.IT_CashIO_History IT_CashIO_History = null;
        private Interface.ServerData.IT_SettlementMoney IT_SettlementMoney = null;
        private Interface.ServerData.IT_PositionDetail_History IT_PositionDetail_History = null;
        private Interface.ServerData.IT_PositionSummaryDelivery IT_PositionSummaryDelivery = null;
        private Interface.ServerData.IT_TradeDetail_History IT_TradeDetail_History = null;
        private Interface.ServerData.IT_TradeDetail_Summary IT_TradeDetail_Summary = null;
        private Interface.ServerData.IT_ClosePositionDetail_History IT_ClosePositionDetail_History = null;
        private Interface.Sys.IDayGroupOperators IDayGroupOperators = null;
        private Interface.Sys.IDayOperatorAccount IDayOperatorAccount = null;
        private Interface.Sys.IDayGroupMaster IDayGroupMaster = null;
        private Interface.Day.IDayWinOrLoseTick IDayWinOrLoseTick = null;
        private Interface.Day.IAccountDayChange IAccountDayChange = null;
        private Interface.Day.IAccountDayAnalysis IAccountDayAnalysis = null;
        private Interface.Day.IAccountDayTradeInstrument IAccountDayTradeInstrument = null;
        private Interface.Day.IAccountDayTradeInstrumentAnalsis IAccountDayTradeInstrumentAnalsis = null;
        private Interface.Day.IAccountDayTradeProduct IAccountDayTradeProduct = null;
        private Interface.Day.IAccountDayTradeProductAnalysis IAccountDayTradeProductAnalysis = null;
        private Interface.Day.IAccountDayPositionDelivery IAccountDayPositionDelivery = null;
        private Interface.Day.IAccountDayMoney IAccountDayMoney = null;
        private Interface.Day.IDayTradeInfo IDayTradeInfo = null;
        private Interface.Month.IAccountMonthChange IAccountMonthChange = null;
        private Interface.Month.IAccountMonthAnalysis IAccountMonthAnalysis = null;
        private Interface.Month.IAccountMonthTradeInstrument IAccountMonthTradeInstrument = null;
        private Interface.Month.IAccountMonthTradeInstrumentAnalysis IAccountMonthTradeInstrumentAnalysis = null;
        private Interface.Month.IAccountMonthTradeProduct IAccountMonthTradeProduct = null;
        private Interface.Month.IAccountMonthTradeProductAnalysis IAccountMonthTradeProductAnalysis = null;
        private Interface.Week.IAccountAvgWeek IAccountAvgWeek = null;
        private Interface.Month.IAccountMonthAvg IAccountMonthAvg = null;
        private Interface.Day.IAccountFiveDayAvg IAccountFiveDayAvg = null;
        private Interface.Day.IBatchAvgDay IBatchAvgDay = null;
        private Interface.Week.IBatchAvgWeek IBatchAvgWeek = null;
        private Interface.Month.IBatchAvgMonth IBatchAvgMonth = null;
        private Interface.Day.IGroupDayChange IGroupDayChange = null;
        private Interface.Day.IGroupAvgDay IGroupAvgDay = null;
        private Interface.Month.IGroupMonthSum IGroupMonthSum = null;
        private Interface.Month.IGroupAvgMonth IGroupAvgMonth = null;
        private Interface.Week.IGroupWeekSum IGroupWeekSum = null;
        private Interface.Year.IGroupYearSum IGroupYearSum = null;
        private Interface.Day.IBrokerageDayTradeInfo IBrokerageDayTradeInfo = null;
        private Interface.Month.IBrokerageMonthTradeInfo IBrokerageMonthTradeInfo = null;
        private Interface.Month.IAccountMonthMoney IAccountMonthMoney = null;
        private Interface.Year.IAccountYearMoney IAccountYearMoney = null;
        private Interface.Month.IAccountMonthExpected IAccountMonthExpected = null;
        private Interface.Week.IAccountWeekExpected IAccountWeekExpected = null;
        private Interface.View.IGroupDayAvg IGroupDayAvg = null;
        private Interface.View.IOperatorAvgWeek IOperatorAvgWeek = null;
        private Interface.View.IOperatorDayChange IOperatorDayChange = null;
        private Interface.View.IOperatorDayDirectionInfo IOperatorDayDirectionInfo = null;
        private Interface.View.IOperatorDayProductInfo IOperatorDayProductInfo = null;
        private Interface.View.IOperatorMonthProductInfo IOperatorMonthProductInfo = null;
        private Interface.View.IOperatorYearProductInfo IOperatorYearProductInfo = null;
        private Interface.View.IOperatorDayMoney IOperatorDayMoney = null;
        private Interface.View.IOperatorMonthMoney IOperatorMonthMoney = null;
        private Interface.View.IOperatorYearMoney IOperatorYearMoney = null;
        private Interface.View.IOperatorDayPosition IOperatorDayPosition = null;
        private Interface.View.IOperatorDayTick IOperatorDayTick = null;
        private Interface.View.IOperatorMonthTick IOperatorMonthTick = null;
        private Interface.View.IOperatorYearTick IOperatorYearTick = null;
        private Interface.View.IOperatorFiveDayAvg IOperatorFiveDayAvg = null;
        private Interface.View.IOperatorMonthAvg IOperatorMonthAvg = null;
        private Interface.Year.IBatchAvgYear IBatchAvgYear = null;
        private Interface.Sys.IOperatorInfo IOperatorInfo = null;
        private Interface.Week.IOperatorWeekSort IOperatorWeekSort = null;
        private Interface.Year.IGroupAvgYear IGroupAvgYear = null;
        private Interface.Day.ICompanyDayChange ICompanyDayChange = null;
        private Interface.Month.IGroupMonthWinOrLoseTick IGroupMonthWinOrLoseTick = null;
        private Interface.Year.IGroupYearWinOrLoseTick IGroupYearWinOrLoseTick = null;
        private Interface.View.IGroupFiveDayAvg IGroupFiveDayAvg = null;
        private Interface.View.IOperatorWeekSum IOperatorWeekSum = null;
        private Interface.View.IOperatorMonthSum IOperatorMonthSum = null;
        private Interface.View.IOperatorYearSum IOperatorYearSum = null;
        private Interface.View.IOperatorYearAvg IOperatorYearAvg = null;
        private Interface.View.IOperatorYearExpected IOperatorYearExpected = null;
        private Interface.View.IOperatorWeekExpected IOperatorWeekExpected = null;
        private Interface.View.IOperatorMonthExpected IOperatorMonthExpected = null;
        private Interface.Day.ICompanyAvgDay ICompanyAvgDay = null;
        private Interface.Week.ICompanyAvgWeek ICompanyAvgWeek = null;
        private Interface.Month.ICompanyAvgMonth ICompanyAvgMonth = null;
        private Interface.Day.ICompanyFiveDayAvg ICompanyFiveDayAvg = null;
        private Interface.Month.ICompanyMonthSum ICompanyMonthSum = null;
        private Interface.Year.ICompanyYearSum ICompanyYearSum = null;
        private Interface.Year.ICompanyYearAvg ICompanyYearAvg = null;
        private Interface.Week.ICompanyWeekSum ICompanyWeekSum = null;
        private Interface.Year.IBrokerageYearTradeInfo IBrokerageYearTradeInfo = null;
        private Interface.Sys.IOperatorCouldSeeGroups IOperatorCouldSeeGroups = null;
        private Interface.Day.IYieldInHoldTime IYieldInHoldTime = null;

        private DB.DBManager db = null;

        public ServiceWorker(DB.DBManager db)
        {
            this.ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IServerStatus = new Service.Sys.ServiceServerStatus(db);
            IExchange = new Service.Sys.ServiceExchange(db);
            IBroker = new Service.Sys.ServiceBroker(db);
            IProduct = new Service.Sys.ServiceProduct(db);
            IInstrument = new Service.Sys.ServiceInstrument(db);
            IAccount = new Service.Sys.ServiceAccount(db);
            IT_CashIO_History = new Service.ServerData.ServiceT_CashIO_History(db);
            IT_SettlementMoney = new Service.ServerData.ServiceT_SettlementMoney(db);
            IT_PositionDetail_History = new Service.ServerData.ServiceT_PositionDetail_History(db);
            IT_PositionSummaryDelivery = new Service.ServerData.ServiceT_PositionSummaryDelivery(db);
            IT_TradeDetail_History = new Service.ServerData.ServiceT_TradeDetail_History(db);
            IT_TradeDetail_Summary = new Service.ServerData.ServiceT_TradeDetail_Summary(db);
            IT_ClosePositionDetail_History = new Service.ServerData.ServiceT_ClosePositionDetail_History(db);
            IDayGroupOperators = new Service.Sys.ServiceDayGroupOperators(db);
            IDayOperatorAccount = new Service.Sys.ServiceDayOperatorAccount(db);
            IDayGroupMaster = new Service.Sys.ServiceDayGroupMaster(db);
            IDayWinOrLoseTick = new Service.Day.ServiceDayWinOrLoseTick(db);
            IAccountDayChange = new Service.Day.ServiceAccountDayChange(db);
            IAccountDayAnalysis = new Service.Day.ServiceAccountDayAnalysis(db);
            IAccountDayTradeInstrument = new Service.Day.ServiceAccountDayTradeInstrument(db);
            IAccountDayTradeInstrumentAnalsis = new Service.Day.ServiceAccountDayTradeInstrumentAnalsis(db);
            IAccountDayTradeProduct = new Service.Day.ServiceAccountDayTradeProduct(db);
            IAccountDayTradeProductAnalysis = new Service.Day.ServiceAccountDayTradeProductAnalysis(db);
            IAccountDayPositionDelivery = new Service.Day.ServiceAccountDayPositionDelivery(db);
            IAccountDayMoney = new Service.Day.ServiceAccountDayMoney(db);
            IDayTradeInfo = new Service.Day.ServiceDayTradeInfo(db);
            IAccountMonthChange = new Service.Month.ServiceAccountMonthChange(db);
            IAccountMonthAnalysis = new Service.Month.ServiceAccountMonthAnalysis(db);
            IAccountMonthTradeInstrument = new Service.Month.ServiceAccountMonthTradeInstrument(db);
            IAccountMonthTradeInstrumentAnalysis = new Service.Month.ServiceAccountMonthTradeInstrumentAnalysis(db);
            IAccountMonthTradeProduct = new Service.Month.ServiceAccountMonthTradeProduct(db);
            IAccountMonthTradeProductAnalysis = new Service.Month.ServiceAccountMonthTradeProductAnalysis(db);
            IAccountAvgWeek = new Service.Week.ServiceAccountAvgWeek(db);
            IAccountMonthAvg = new Service.Month.ServiceAccountMonthAvg(db);
            IAccountFiveDayAvg = new Service.Day.ServiceAccountFiveDayAvg(db);
            IBatchAvgDay = new Service.Day.ServiceBatchAvgDay(db);
            IBatchAvgWeek = new Service.Week.ServiceBatchAvgWeek(db);
            IBatchAvgMonth = new Service.Month.ServiceBatchAvgMonth(db);
            IGroupDayChange = new Service.Day.ServiceGroupDayChange(db);
            IGroupAvgDay = new Service.Day.ServiceGroupAvgDay(db);
            IGroupMonthSum = new Service.Month.ServiceGroupMonthSum(db);
            IGroupAvgMonth = new Service.Month.ServiceGroupAvgMonth(db);
            IGroupWeekSum = new Service.Week.ServiceGroupWeekSum(db);
            IGroupYearSum = new Service.Year.ServiceGroupYearSum(db);
            IBrokerageDayTradeInfo = new Service.Day.ServiceBrokerageDayTradeInfo(db);
            IBrokerageMonthTradeInfo = new Service.Month.ServiceBrokerageMonthTradeInfo(db);
            IAccountMonthMoney = new Service.Month.ServiceAccountMonthMoney(db);
            IAccountYearMoney = new Service.Year.ServiceAccountYearMoney(db);
            IAccountMonthExpected = new Service.Month.ServiceAccountMonthExpected(db);
            IGroupDayAvg = new Service.View.ServiceGroupDayAvg(db);
            IOperatorAvgWeek = new Service.View.ServiceOperatorAvgWeek(db);
            IOperatorDayDirectionInfo = new Service.View.ServiceOperatorDayDirectionInfo(db);
            IOperatorDayProductInfo = new Service.View.ServiceOperatorDayProductInfo(db);
            IOperatorMonthProductInfo = new Service.View.ServiceOperatorMonthProductInfo(db);
            IOperatorYearProductInfo = new Service.View.ServiceOperatorYearProductInfo(db);
            IOperatorDayMoney = new Service.View.ServiceOperatorDayMoney(db);
            IOperatorMonthMoney = new Service.View.ServiceOperatorMonthMoney(db);
            IOperatorYearMoney = new Service.View.ServiceOperatorYearMoney(db);
            IOperatorDayPosition = new Service.View.ServiceOperatorDayPosition(db);
            IOperatorDayTick = new Service.View.ServiceOperatorDayTick(db);
            IOperatorMonthTick = new Service.View.ServiceOperatorMonthTick(db);
            IOperatorYearTick = new Service.View.ServiceOperatorYearTick(db);
            IOperatorFiveDayAvg = new Service.View.ServiceOperatorFiveDayAvg(db);
            IOperatorMonthAvg = new Service.View.ServiceOperatorMonthAvg(db);
            IBatchAvgYear = new Service.Year.ServiceBatchAvgYear(db);
            IOperatorInfo = new Service.Sys.ServiceOperatorInfo(db);
            IOperatorWeekSort = new Service.Week.ServiceOperatorWeekSort(db);
            IGroupAvgYear = new Service.Year.ServiceGroupAvgYear(db);
            ICompanyDayChange = new Service.Day.ServiceCompanyDayChange(db);
            IGroupMonthWinOrLoseTick = new Service.Month.ServiceGroupMonthWinOrLoseTick(db);
            IGroupFiveDayAvg = new Service.View.ServiceGroupFiveDayAvg(db);
            IOperatorWeekSum = new Service.View.ServiceOperatorWeekSum(db);
            IOperatorMonthSum = new Service.View.ServiceOperatorMonthSum(db);
            IOperatorYearSum = new Service.View.ServiceOperatorYearSum(db);
            IOperatorYearAvg = new Service.View.ServiceOperatorYearAvg(db);
            IOperatorYearExpected = new Service.View.ServiceOperatorYearExpected(db);
            IOperatorWeekExpected = new Service.View.ServiceOperatorWeekExpected(db);
            IOperatorMonthExpected = new Service.View.ServiceOperatorMonthExpected(db);
            ICompanyAvgDay = new Service.Day.ServiceCompanyAvgDay(db);
            ICompanyAvgWeek = new Service.Week.ServiceCompanyAvgWeek(db);
            ICompanyAvgMonth = new Service.Month.ServiceCompanyAvgMonth(db);
            ICompanyFiveDayAvg = new Service.Day.ServiceCompanyFiveDayAvg(db);
            ICompanyMonthSum = new Service.Month.ServiceCompanyMonthSum(db);
            ICompanyYearSum = new Service.Year.ServiceCompanyYearSum(db);
            ICompanyYearAvg = new Service.Year.ServiceCompanyYearAvg(db);
            ICompanyWeekSum = new Service.Week.ServiceCompanyWeekSum(db);
            IBrokerageYearTradeInfo = new Service.Year.ServiceBrokerageYearTradeInfo(db);
            IOperatorCouldSeeGroups = new Service.Sys.ServiceOperatorCouldSeeGroups(db);
            IYieldInHoldTime = new Service.Day.ServiceYieldInHoldTime(db);

            this.db = db;
        }

        /// <summary>
        /// 导入某日数据
        /// </summary>
        /// <param name="pdate"></param>
        public void Work(string pdate)
        {
            //Models.Sys.TradeHistory th = new Sys.TradeHistory();
            if (!ITradeHistory.HasChangeIntoDb(pdate))
            {
                #region 数据库备份
                this.BackUpAtDay(pdate.Replace("-", ""));
                #endregion


                #region 保存导入时间
                int tradeHistoryId = ITradeHistory.Insert(pdate);//访问数据库

                DateTime dd = Convert.ToDateTime(pdate.Substring(0, 4) + "-" + pdate.Substring(4, 2) + "-" + pdate.Substring(6));

                #endregion

                #region 开始结算时设定系统忙，不能被查询
                IServerStatus.SetServerStatus(Models.Sys._ServerStatus.Bussy);
                #endregion



                #region 保存Server中新的交易所
                List<Models.ServerData.T_Exchange> exchanges = this.GetUnRegistExchanges();
                foreach (Models.ServerData.T_Exchange exchange in exchanges)
                {
                    IExchange.InsertIntoBenefitFromServer(exchange.ExchangeID, exchange.ExchangeName);
                }
                #endregion

                #region 保存Server中新的经纪公司
                IBroker.InitBrokers();
                #endregion

                #region 保存Server中新的品种
                List<Models.ServerData.T_Product> products = this.GetUnRegistProducts(db);
                foreach (Models.ServerData.T_Product product in products)
                {
                    IProduct.InsertIntoBenefitFromServer(product.ProductID, product.ExchangeID);
                }
                #endregion

                #region 保存Server中新的合约
                List<Models.ServerData.T_Instrument> instruments = this.GetUnRegistInstruments();
                foreach (Models.ServerData.T_Instrument instrument in instruments)
                {
                    IInstrument.InsertIntoBenefitFromServer(instrument.ProductID, instrument.ExchangeID, instrument.InstrumentID);
                }
                #endregion

                #region 保存Server中新的账号
                List<Models.ServerData.T_Account> accounts = this.GetUnRegistAccounts(db);
                foreach (Models.ServerData.T_Account account in accounts)
                {
                    IAccount.InsertIntoBenefitFromServer(account.LoginAccount);
                }
                #endregion



                //#region 这里应该检查操作员,账户对应关系，以及组是否已经存在，如果不存在，则创建默认操作员和组（这一步只在开发过程中使用）
                //bool couldCountion = new BenefitHelper.Data.ServerData.T_Account().AccountCountIsSameToBenefit(db);
                //if (!couldCountion)
                //{
                //    db.Dispose();
                //    "平台账户数量与分析系统不一致，需要绑定新的账户".Error();
                //    System.Environment.Exit(System.Environment.ExitCode);
                //}
                //#endregion


                #region 保存Server中新的出入金明细
                IT_CashIO_History.CopyServerCashIOHistoryDataIntoBenefit(pdate);
                #endregion

                #region 保存Server中新的结算资金信息
                IT_SettlementMoney.CopyServerSettlementMoneyDataIntoBenefit(pdate);
                #endregion

                #region 保存Server中新的持仓明细
                IT_PositionDetail_History.CopyServerPositionDetailHistoryDataIntoBenefit(pdate);
                #endregion

                #region 保存Server中新的持仓汇总
                IT_PositionSummaryDelivery.CopyServerPositionSummaryDataIntoBenefit(pdate);
                #endregion

                #region 保存Server中新的成交明细
                IT_TradeDetail_History.CopyServerTradeDetailHistoryDataIntoBenefit(pdate);
                #endregion

                #region 保存Server中新的成交汇总
                IT_TradeDetail_Summary.CopyServerTradeSummaryDataIntoBenefit(pdate);
                #endregion

                #region 保存Server中新的平仓明细
                IT_ClosePositionDetail_History.CopyServerClosePositionDetailHistoryDataIntoBenefit(pdate);
                #endregion



                #region 初始化每天的小组交易员关系
                IDayGroupOperators.InitDayGroupOperators(dd);
                #endregion

                #region 初始化每天的账户交易员关系
                IDayOperatorAccount.InitDayOperatorAccount(dd);
                #endregion

                #region 初始化每天的组长组关系
                IDayGroupMaster.InitDayGroupMaster(dd);
                #endregion



                #region 开始分析数据

                #region 交易合约Tick值ok
                IDayWinOrLoseTick.InitTickValue(pdate, tradeHistoryId);
                #endregion

                #region 账号数据变动基本信息ok
                IAccountDayChange.InitDayAccountDayChange(tradeHistoryId, pdate);
                #endregion

                #region 账号数据变动分析信息ok
                IAccountDayAnalysis.InitAccountDayAnalysis(tradeHistoryId, pdate);
                #endregion

                #region 账户合约数据变动基本信息ok
                IAccountDayTradeInstrument.InitDayAccountInstrumentChange(tradeHistoryId, pdate);
                #endregion

                #region 账户合约数据变动分析信息ok
                IAccountDayTradeInstrumentAnalsis.InitAccountInstrumentDayAnalysis(tradeHistoryId, pdate);
                #endregion

                #region 账户品种数据变动基本信息ok
                IAccountDayTradeProduct.InitDayAccountProductChange(tradeHistoryId, pdate);
                #endregion

                #region 账户品种数据变动分析信息ok
                IAccountDayTradeProductAnalysis.InitAccountProductDayAnalysis(tradeHistoryId, pdate);
                #endregion

                #region 账户持仓信息ok
                IAccountDayPositionDelivery.InitAccountDayPositionDelivery(tradeHistoryId, pdate);
                #endregion

                #region 账户资金信息ok
                IAccountDayMoney.InitDayAccountDayMoney(tradeHistoryId, pdate);
                #endregion

                #region 账户分时数据ok
                IDayTradeInfo.InitDayStateChange(tradeHistoryId, pdate);
                #endregion

                #region 以下与基本数据无关

                #region 账户月基本数据
                Models.Sys.TradeHistory th = ITradeHistory.GetModel(tradeHistoryId);
                IAccountMonthChange.InitMonthAccountChange(th.MPdate, th.YPdate);
                #endregion

                #region 账户月分析数据
                IAccountMonthAnalysis.InitMonthAccountAnalysis(th.MPdate, th.YPdate);
                #endregion

                #region 账户月合约基本数据
                IAccountMonthTradeInstrument.InitMonthAccountInstrumentChange(th.MPdate, th.YPdate);
                #endregion

                #region 账户月合约分析数据
                IAccountMonthTradeInstrumentAnalysis.InitMonthAccountInstrumentAnalysis(th.MPdate, th.YPdate);
                #endregion

                #region 账户月品种基本数据
                IAccountMonthTradeProduct.InitMonthAccountProductChange(th.MPdate, th.YPdate);
                #endregion

                #region 账户月品种分析数据
                IAccountMonthTradeProductAnalysis.InitMonthAccountProductAnalysis(th.MPdate, th.YPdate);
                #endregion

                #region 账户周均值数据
                IAccountAvgWeek.InitAccountWeekAvg(th.WPdate, th.MPdate, th.YPdate);
                #endregion

                #region 账户月均值数据
                IAccountMonthAvg.InitAccountMonthAvg(th.MPdate, th.YPdate);
                #endregion



                #region 账户五日均值
                IAccountFiveDayAvg.InitAccountFiveDayAvg(tradeHistoryId, pdate);
                #endregion

                #region 同批次交易员日均值
                IBatchAvgDay.InitBatchDayAvg(tradeHistoryId, pdate);
                #endregion

                #region 同批次交易员周均值
                IBatchAvgWeek.InitBatchWeekAvg(th.WPdate, th.MPdate, th.YPdate);
                #endregion

                #region 同批次交易员月均值
                IBatchAvgMonth.InitBatchMonthAvg(th.MPdate, th.YPdate);
                #endregion

                #region 小组日累计值
                IGroupDayChange.InitGroupDaySum(tradeHistoryId, pdate);
                #endregion

                #region 小组日均值
                IGroupAvgDay.InitGroupDayAvg(tradeHistoryId);
                #endregion



                #region 小组月累计值
                IGroupMonthSum.InitGroupMonthSum(th.MPdate, th.YPdate);
                #endregion

                #region 小组月均值
                IGroupAvgMonth.InitGroupMonthAvg(th.MPdate, th.YPdate);
                #endregion

                #region 小组周累计值
                IGroupWeekSum.InitGroupWeekSum(th.WPdate, th.MPdate, th.YPdate);
                #endregion

                #region 小组年累计值
                IGroupYearSum.InitGroupYearSum(th.YPdate);
                #endregion

                #region 经纪公司日交易情况
                IBrokerageDayTradeInfo.InitBrokerDayTradeInfo(th.Id, pdate);
                #endregion

                #region 经纪公司月交易情况
                IBrokerageMonthTradeInfo.InitBrokerMonthTradeInfo(th.MPdate, th.YPdate);
                #endregion


                #region 账户月资金信息
                IAccountMonthMoney.InitAccountMonthMoney(th.MPdate, th.YPdate);
                #endregion

                #region 账户年资金信息
                IAccountYearMoney.InitAccountYearMoney(th.YPdate);
                #endregion


                #region 账户周预期
                IAccountMonthExpected.InitAccountMonthExpected(th.MPdate, th.YPdate);
                #endregion


                #region 账户月预期
                IAccountWeekExpected.InitAccountWeekExpected(th.WPdate, th.MPdate, th.YPdate);
                #endregion

                #endregion


                #endregion






                #region 初始化小组日均值
                IGroupDayAvg.InitGroupAvg(tradeHistoryId);
                #endregion

                #region 操作员周均值
                IOperatorAvgWeek.InitOperatorAvgWeek(tradeHistoryId);
                #endregion

                #region 初始化交易员日分析数据
                IOperatorDayChange.InitOperatorDayChange(tradeHistoryId);
                #endregion

                #region 初始化交易员日多空数据
                IOperatorDayDirectionInfo.InitOperatorDayDirectionInfo(tradeHistoryId, false);
                IOperatorDayDirectionInfo.InitOperatorDayDirectionInfo(tradeHistoryId, true);
                #endregion

                #region 初始化交易员日品种交易情况
                IOperatorDayProductInfo.IntiOperatorDayProductInfo(tradeHistoryId);
                #endregion

                #region 初始化交易员月品种交易情况
                IOperatorMonthProductInfo.InitOperatorMonthProductInfo(tradeHistoryId);
                #endregion

                #region 初始化交易员年品种交易情况
                IOperatorYearProductInfo.InitOperatorYearProductInfo(tradeHistoryId);
                #endregion

                #region 初始化交易员日资金信息
                IOperatorDayMoney.InitOperatorDayMoney(tradeHistoryId);
                #endregion

                #region 初始化交易员月资金信息
                IOperatorMonthMoney.InitOperatorMonthMoney(th.MPdate, th.YPdate);
                #endregion

                #region 初始化交易员年资金信息
                IOperatorYearMoney.InitOperatorYearMoney(th.YPdate);
                #endregion

                #region 初始化交易员持仓信息
                IOperatorDayPosition.InitOperatorDayPosition(tradeHistoryId);
                #endregion

                #region 初始化交易员Tick数据
                IOperatorDayTick.InitOperatorDayTick(tradeHistoryId, true);
                IOperatorDayTick.InitOperatorDayTick(tradeHistoryId, false);
                #endregion


                #region 初始化交易员月Tick数据
                IOperatorMonthTick.InitOperatorMonthTick(th.MPdate, th.YPdate, true);
                IOperatorMonthTick.InitOperatorMonthTick(th.MPdate, th.YPdate, false);
                #endregion

                #region 初始化交易员年Tick数据
                IOperatorYearTick.InitOperatorYearTick(th.YPdate, true);
                IOperatorYearTick.InitOperatorYearTick(th.YPdate, false);
                #endregion

                #region 初始化交易员五日均值
                IOperatorFiveDayAvg.InitOperatorFiveDayAvg(tradeHistoryId);
                #endregion

                #region 初始化交易员月值
                IOperatorMonthAvg.InitOperatorMonthAvg(tradeHistoryId);
                #endregion

                #region 初始化同批次交易员年均值
                IBatchAvgYear.InitBatchAvgYear(th.YPdate);
                #endregion

                #region  初始化交易员交易情况
                IOperatorInfo.OperatorWorking(tradeHistoryId);
                #endregion

                #region 初始化交易员小组排名
                IOperatorWeekSort.InitOperatorWeekSort(tradeHistoryId);
                #endregion

                #region 初始化小组年均值
                IGroupAvgYear.InitGroupAvgYear(th.YPdate);
                #endregion

                #region 初始化公司日总值
                ICompanyDayChange.InitCompanyDayChange(th.Id);
                #endregion





                #region 初始化小组月Tick值
                IGroupMonthWinOrLoseTick.InitGroupMonthWinOrLoseTick(th.Id, true);
                IGroupMonthWinOrLoseTick.InitGroupMonthWinOrLoseTick(th.Id, false);
                #endregion

                #region 初始化小组年Tick值
                IGroupYearWinOrLoseTick.InitGroupYearWinOrLoseTick(th.Id, true);
                IGroupYearWinOrLoseTick.InitGroupYearWinOrLoseTick(th.Id, false);
                #endregion

                #region 初始化小组五日均值
                IGroupFiveDayAvg.InitGroupFiveDayAvg(tradeHistoryId);
                #endregion



                #region 初始化交易员周总计
                IOperatorWeekSum.InitOperatorWeekSum(tradeHistoryId);
                #endregion

                #region 初始化交易月总计
                IOperatorMonthSum.InitOperatorMonthSum(th.MPdate, th.YPdate);
                #endregion

                #region 初始化交易员年总计
                IOperatorYearSum.InitOperatorYearSum(th.Id);
                #endregion

                #region 初始化交易员年均值
                IOperatorYearAvg.InitOperatorYearAvg(th.Id);
                #endregion

                #region 初始化交易员年预期
                IOperatorYearExpected.InitOperatorYearExpected(th.Id);
                #endregion

                #region 初始化交易员周预期
                IOperatorWeekExpected.InitOperatorWeekExpected(th.Id);
                #endregion

                #region  初始化交易员月预期
                IOperatorMonthExpected.InitOperatorMonthExpected(th.Id);
                #endregion

                #region 公司日均值数据
                ICompanyAvgDay.InitCompanyDayAvg(th.Id);
                #endregion

                #region 公司周均值数据
                ICompanyAvgWeek.InitCompanyWeekAvg(th.WPdate, th.MPdate, th.YPdate);
                #endregion

                #region 公司月均值数据
                ICompanyAvgMonth.InitCompanyMonthAvg(th.MPdate, th.YPdate);
                #endregion

                #region 公司五日均值数据
                ICompanyFiveDayAvg.InitCompanyFiveDayAvg(th.Id);
                #endregion

                #region 初始化公司月总值
                ICompanyMonthSum.InitCompanyMonthSum(th.YPdate, th.MPdate);
                #endregion


                #region 初始化公司年总值
                ICompanyYearSum.InitCompanyYearSum(th.YPdate);
                #endregion

                #region 初始化公司年均值
                ICompanyYearAvg.InitCompanyYearAvg(th.YPdate);
                #endregion

                #region 初始化公司周累计
                ICompanyWeekSum.InitCompanyWeekSum(th.YPdate, th.WPdate, th.MPdate);
                #endregion

                #region 初始化经济公司年累计
                IBrokerageYearTradeInfo.InitBrokerYearTradeInfo(th.YPdate);
                #endregion
                //  new Sys.OperatorCouldSeeGroups().Delete(db);
                IOperatorCouldSeeGroups.InitOperatorCouldSeeGroups();
                #region 结束结算时设定系统空闲
                IServerStatus.SetServerStatus(Models.Sys._ServerStatus.Free);
                #endregion
            }
        }
        /// <summary>
        /// 清除某日交易记录
        /// </summary>
        /// <param name="pdate"></param>
        public void Clear(string pdate)
        {


            #region 删除Server中新的出入金明细
            IT_CashIO_History.DeleteDayCashIOFromBenefit(pdate);
            #endregion

            #region 删除Server中新的结算信息
            IT_SettlementMoney.DeleteDaySettlementMoneyFromBenefit(pdate);
            #endregion

            #region 删除Server中新的持仓明细
            IT_PositionDetail_History.DeleteDayPositionDetailHistoryFromBenefit(pdate);
            #endregion

            #region 删除Server中新的持仓汇总
            IT_PositionSummaryDelivery.DeleteDayPositionSummaryFromBenefit(pdate);
            #endregion

            #region 删除Server中新的成交明细
            IT_TradeDetail_History.DeleteDayTradeDetailHistoryFromBenefit(pdate);
            #endregion

            #region 删除Server中新的成交汇总
            IT_TradeDetail_Summary.DeleteDayTradeSummaryFromBenefit(pdate);
            #endregion

            #region 删除Server中新的平仓明细
            IT_ClosePositionDetail_History.DeleteDayClosePositionDetailHistoryFromBenefit(pdate);
            #endregion

            #region 删除当日的分组情况
            int tradeHistoryId = ITradeHistory.GetSettlementModel(pdate).Id;
            Models.Sys.TradeHistory th = ITradeHistory.GetModel(tradeHistoryId);
            if (tradeHistoryId == 0)
            {
                return;
            }
            DateTime dd = ITradeHistory.GetDateTimeFromTradeHistoryId(tradeHistoryId);
            IDayGroupOperators.DeleteDayGroupOperators(dd);
            #endregion

            #region 当日有交易的交易员交易天数减一
            IOperatorInfo.UnsetDayWorking(tradeHistoryId);
            #endregion

            #region 删除当日的交易员账户分配情况
            IDayOperatorAccount.DeleteDayOperatorAccount(dd);
            #endregion

            #region 删除当日的组长情况
            IDayGroupMaster.DeleteDayGroupMaster(dd);
            #endregion

            #region 删除分析数据

            #region 删除数据变动基本信息
            IAccountDayChange.Delete(tradeHistoryId);
            #endregion

            #region 删除数据变动分析信息
            IAccountDayAnalysis.Delete(tradeHistoryId);
            #endregion

            #region 删除Tick分析信息
            IDayWinOrLoseTick.Delete(tradeHistoryId);
            #endregion

            #region 删除账户合约数据变动基本信息
            IAccountDayTradeInstrument.Delete(tradeHistoryId);
            #endregion

            #region 删除账户合约数据变动分析信息
            IAccountDayTradeInstrumentAnalsis.Delete(tradeHistoryId);
            #endregion

            #region 删除账户品种数据变动基本信息
            IAccountDayTradeProduct.Delete(tradeHistoryId);
            #endregion

            #region 删除账户品种数据变动分析信息
            IAccountDayTradeProductAnalysis.Delete(tradeHistoryId);
            #endregion

            #region 删除日持仓信息
            IAccountDayPositionDelivery.Delete(tradeHistoryId);
            #endregion

            #region 删除账户日资金信息
            IAccountDayMoney.Delete(tradeHistoryId);
            #endregion

            #region 删除账户月基本数据
            IAccountMonthChange.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除账户月分析数据
            IAccountMonthAnalysis.Delete(th.MPdate, th.YPdate);
            #endregion


            #region 删除账户月合约基本数据
            IAccountMonthTradeInstrument.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除账户月合约分析数据
            IAccountMonthTradeInstrumentAnalysis.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除账户月品种基本数据
            IAccountMonthTradeProduct.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除账户月品种分析数据
            IAccountMonthTradeProductAnalysis.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除账户周均值数据
            IAccountAvgWeek.Delete(th.WPdate, th.MPdate, th.YPdate);
            #endregion

            #region 删除账户月均值数据
            IAccountMonthAvg.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除公司日均值数据
            ICompanyAvgDay.Delete(th.Id);
            #endregion

            #region 删除公司周均值数据
            ICompanyAvgWeek.Delete(th.WPdate, th.MPdate, th.YPdate);
            #endregion

            #region 删除公司月均值数据
            ICompanyAvgMonth.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除账户五日均值
            IAccountFiveDayAvg.Delete(tradeHistoryId);
            #endregion

            #region 删除同批次交易员日均值
            IBatchAvgDay.Delete(tradeHistoryId);
            #endregion

            #region 删除同批次交易员周均值
            IBatchAvgWeek.Delete(th.WPdate, th.MPdate, th.YPdate);
            #endregion

            #region 删除同批次交易员月均值
            IBatchAvgMonth.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除小组日累计值
            IGroupDayChange.Delete(th.Id);
            #endregion

            #region 删除小组月累计值
            IGroupMonthSum.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除小组日均值
            IGroupAvgDay.Delete(tradeHistoryId);
            #endregion

            #region 删除小组月累计值
            IGroupAvgMonth.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除小组月Tick值
            IGroupMonthWinOrLoseTick.Delete(th.Id);
            #endregion

            #region 删除小组年Tick值
            IGroupYearWinOrLoseTick.Delete(th.Id);
            #endregion

            #region 删除小组周累计值
            IGroupWeekSum.Delete(th.WPdate, th.MPdate, th.YPdate);
            #endregion

            #region 删除小组年累计值
            IGroupYearSum.Delete(th.YPdate);
            #endregion

            #region 删除分时数据
            IDayTradeInfo.Delete(th.Id);
            #endregion


            #region 删除经纪公司日交易情况
            IBrokerageDayTradeInfo.Delete(th.Id);
            #endregion

            #region 删除经纪公司月交易情况
            IBrokerageMonthTradeInfo.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除账户月资金信息
            IAccountMonthMoney.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除账户年资金信息
            IAccountYearMoney.Delete(th.YPdate);
            #endregion


            #region 删除账户周预期
            IAccountMonthExpected.Delete(th.MPdate, th.YPdate);
            #endregion


            #region 删除账户月预期
            IAccountWeekExpected.Delete(th.WPdate, th.MPdate, th.YPdate);
            #endregion

            #region 删除收益率持仓时间分析数据
            IYieldInHoldTime.DeleteYieldHoldTime(tradeHistoryId);
            #endregion

            #region 删除小组日均值
            IGroupDayAvg.Delete(tradeHistoryId);
            #endregion

            #region 删除小组五日均值
            IGroupFiveDayAvg.Delete(tradeHistoryId);
            #endregion

            #region 删除同批次交易员年均值
            IBatchAvgYear.Delete(th.YPdate);
            #endregion


            #region 删除小组年均值
            IGroupAvgYear.Delete(th.YPdate);
            #endregion

            #region 删除公司年均值
            ICompanyYearAvg.Delete(th.YPdate);
            #endregion

            #region 删除公司日数据
            ICompanyDayChange.Delete(th.Id);
            #endregion

            #region 删除公司月数据
            ICompanyMonthSum.Delete(th.YPdate, th.MPdate);
            #endregion

            #region 删除公司年数据
            ICompanyYearSum.Delete(th.YPdate);
            #endregion

            #region 删除交易员周均值
            IOperatorAvgWeek.Delete(tradeHistoryId);
            #endregion

            #region 删除交易员日分析数据
            IOperatorDayChange.Delete(tradeHistoryId);
            #endregion

            #region 删除交易员日多空数据
            IOperatorDayDirectionInfo.Delete(tradeHistoryId);
            #endregion

            #region 删除交易员日资金信息
            IOperatorDayMoney.Delete(tradeHistoryId);
            #endregion


            #region 初始化交易员月资金信息
            IOperatorMonthMoney.Delete(th.YPdate, th.MPdate);
            #endregion

            #region 初始化交易员年资金信息
            IOperatorYearMoney.Delete(th.YPdate);
            #endregion

            #region 删除化交易员持仓信息
            IOperatorDayPosition.Delete(tradeHistoryId);
            #endregion

            #region 删除交易员Tick数据
            IOperatorDayTick.Delete(tradeHistoryId);
            #endregion

            #region 删除交易员月Tick数据
            IOperatorMonthTick.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除交易员年Tick数据
            IOperatorYearTick.Delete(th.YPdate);
            #endregion

            #region 删除交易员日品种交易情况
            IOperatorDayProductInfo.Delete(tradeHistoryId);
            #endregion

            #region 删除交易员月品种交易情况
            IOperatorMonthProductInfo.Delete(tradeHistoryId);
            #endregion

            #region 删除交易员年品种交易情况
            IOperatorYearProductInfo.Delete(tradeHistoryId);
            #endregion

            #region 删除交易员五日均值
            IOperatorFiveDayAvg.Delete(tradeHistoryId);
            #endregion

            #region 删除交易员月值
            IOperatorMonthAvg.Delete(th.YPdate, th.MPdate);
            #endregion

            #region 删除交易员周总计
            IOperatorWeekSum.Delete(th.YPdate, th.MPdate, th.WPdate);
            #endregion


            #region 删除交易月总计
            IOperatorMonthSum.Delete(th.MPdate, th.YPdate);
            #endregion

            #region 删除交易员年总计
            IOperatorYearSum.Delete(th.YPdate);
            #endregion

            #region 删除交易员年均值
            IOperatorYearAvg.Delete(th.YPdate);
            #endregion

            #region 删除交易员年预期
            IOperatorYearExpected.Delete(th.Id);
            #endregion

            #region 删除交易员周预期
            IOperatorWeekExpected.Delete(th.YPdate, th.MPdate, th.WPdate);
            #endregion

            #region 删除交易员月预期
            IOperatorMonthExpected.Delete(th.YPdate, th.MPdate);
            #endregion

            #region 删除公司五日均值
            ICompanyFiveDayAvg.Delete(th.Id);
            #endregion

            #region 删除公司周累计
            ICompanyWeekSum.Delete(th.YPdate, th.WPdate);
            #endregion

            #region 删除经济公司年累计
            IBrokerageYearTradeInfo.Delete(th.YPdate);
            #endregion

            #endregion

            //   new BenefitHelper.Sys.OperatorCouldSeeGroups().Delete(db);

            #region 删除保存时间
            ITradeHistory.Delete(pdate);
            #endregion
        }

        #region 得到还没有在分析系统存在的账号
        /// <summary>
        /// 得到还没有在分析系统存在的账号
        /// </summary>
        /// <returns></returns>
        private List<Models.ServerData.T_Account> GetUnRegistAccounts(DB.DBManager db)
        {
            Interface.ServerData.IT_Account IT_Account = new Service.ServerData.ServiceT_Account(db);
            List<Models.ServerData.T_Account> accounts = new List<Models.ServerData.T_Account>();//保存还未注册的账户
            List<Models.ServerData.T_Account> serverAccounts = IT_Account.GetList();//获取所有账户
            List<Models.Sys.Account> benefitaccounts = IAccount.GetList();
            foreach (Models.ServerData.T_Account t in serverAccounts)
            {
                bool isExist = false;
                var query = from s in benefitaccounts where t.LoginAccount.Equals(s.AccountName) select s;
                if (query.Count() > 0)
                {
                    isExist = true;
                }
                if (!isExist)
                {
                    accounts.Add(t);
                }
            }
            IT_Account = null;
            return accounts;
        }
        #endregion

        #region 得到还没有在分析系统存在的品种
        /// <summary>
        /// 得到还没有在分析系统存在的品种
        /// </summary>
        /// <returns></returns>
        private List<Models.ServerData.T_Product> GetUnRegistProducts(DB.DBManager db)
        {
            Interface.ServerData.IT_Product IT_Product = new Service.ServerData.ServiceT_Product(db);
            List<Models.ServerData.T_Product> products = new List<Models.ServerData.T_Product>();//保存还未注册的品种
            List<Models.ServerData.T_Product> serverProducts = IT_Product.GetList();//获取所有品种
            List<Models.Sys.Product> benefitproducts = IProduct.GetList();
            foreach (Models.ServerData.T_Product t in serverProducts)
            {
                bool isExist = false;
                var query = from s in benefitproducts where t.ProductID.Equals(s.ProductId) select s;
                if (query.Count() > 0)
                {
                    isExist = true;
                }
                if (!isExist)
                {
                    products.Add(t);
                }
            }
            IT_Product = null;
            return products;
        }
        #endregion

        #region 得到还没有在分析系统存在的合约
        /// <summary>
        /// 得到还没有在分析系统存在的合约
        /// </summary>
        /// <returns></returns>
        private List<Models.ServerData.T_Instrument> GetUnRegistInstruments()
        {
            Interface.ServerData.IT_Instrument IT_Instrument = new Service.ServerData.ServiceT_Instrument(db);
            List<Models.ServerData.T_Instrument> instruments = new List<Models.ServerData.T_Instrument>();//保存还未注册的合约
            List<Models.ServerData.T_Instrument> serverInstruments = IT_Instrument.GetList();//获取所有合约
            List<Models.Sys.Instrument> benefitinstruments = IInstrument.GetList();
            foreach (Models.ServerData.T_Instrument t in serverInstruments)
            {
                bool isExist = false;
                var query = from s in benefitinstruments where t.InstrumentID.Equals(s.InstrumentId) select s;
                if (query.Count() > 0)
                {
                    isExist = true;
                }
                if (!isExist)
                {
                    instruments.Add(t);
                }
            }
            IT_Instrument = null;
            return instruments;
        }
        #endregion

        #region 得到还没有在分析系统存在的交易所
        /// <summary>
        /// 得到还没有在分析系统存在的交易所
        /// </summary>
        /// <returns></returns>
        private List<Models.ServerData.T_Exchange> GetUnRegistExchanges()
        {
            Interface.ServerData.IT_Exchange ITExchange = new Service.ServerData.ServiceT_Exchange(db);

            List<Models.ServerData.T_Exchange> exchanges = new List<Models.ServerData.T_Exchange>();//保存还未注册的合约
            List<Models.ServerData.T_Exchange> serverExchanges = ITExchange.GetList();//获取所有server保存的交易所
            List<Models.Sys.Exchange> benefitExchanges = IExchange.GetList();
            foreach (Models.ServerData.T_Exchange t in serverExchanges)
            {
                bool isExist = false;
                var query = from s in benefitExchanges where t.ExchangeID.Equals(s.ExchangeId) select s;
                if (query.Count() > 0)
                {
                    isExist = true;
                }
                if (!isExist)
                {
                    exchanges.Add(t);
                }
            }
            ITExchange = null;
            return exchanges;
        }
        #endregion




        private void BackUpAtDay(string pdate)
        {
            string dpath = AppDomain.CurrentDomain.BaseDirectory + @"Backup\";
            if (!System.IO.Directory.Exists(dpath))
            {
                System.IO.Directory.CreateDirectory(dpath);
            }
            SqlParameter parameter = new SqlParameter();
            parameter.SqlDbType = SqlDbType.VarChar;
            parameter.ParameterName = "pdate";
            parameter.Value = pdate;
            SqlParameter parameter2 = new SqlParameter();
            parameter2.SqlDbType = SqlDbType.VarChar;
            parameter2.ParameterName = "path";
            parameter2.Value = dpath;
            db.Database.ExecuteSqlCommand("EXECUTE backupnewbenefit @pdate,@path", parameter, parameter2);
        }

        private void RestoreAtDay(string pdate)
        {
            string dpath = AppDomain.CurrentDomain.BaseDirectory + @"Backup\";
            SqlParameter parameter2 = new SqlParameter();
            parameter2.SqlDbType = SqlDbType.VarChar;
            parameter2.ParameterName = "path";
            parameter2.Value = dpath;
            string filepath = dpath + "newbenefit_" + pdate + ".bak";
            if (System.IO.File.Exists(filepath))
            {
                string sql = "ALTER DATABASE [newbenefit] SET OFFLINE WITH ROLLBACK IMMEDIATE;use master restore database newbenefit from disk='" + filepath + "' WITH FILE = 1,REPLACE, STATS = 10";
                db.Database.ExecuteSqlCommand(sql);
            }
        }
    }
}
